import pandas as pd
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.linear_model import LinearRegression
plt.rcParams['font.sans-serif'] = 'SimHei'
df = pd.read_excel('最新发布的北京二手房数据_预处理.xlsx')
unit_price = df['单价（元/平方米）']
house_area = df['面积（平方米）']
house_type = df[['室', '厅']]
house_regin = df[['通州', '朝阳', '昌平', '顺义', '丰台', '海淀', '西城', '房山', '石景山', '大兴', '怀柔', '东城', '门头沟', '密云', '延庆', '平谷', '亦庄开发区']]
house_finish = [['毛坯', '简装', '精装']]
house_structure = df[['塔楼', '板楼', '板塔结合', '平房']]
is_subway = df[['近地铁', '不近地铁']]
house_dirt = df[['东', '南', '西', '北', '东北', '东南', '西南', '西北']]
house_year = df['房龄']
x = pd.concat([house_area, house_type, house_regin, house_finish, house_structure, is_subway,house_dirt, house_year], axis=1)
y = unit_price
x_train,x_test,y_train,y_test=model_selection.train_test_split(x,y, test_size=0.2)
LR = LinearRegression()
reg = LR.fit(x_train,y_train)
predicted = reg.predict(x_test)
plt.figure(figsize=(12,6))
n = 50
plt.plot(range(n), y_test[-n:], '-.*')
plt.plot(range(n), predicted[-n:], 'r--.')
plt.legend(['实际值', '估计值'])
plt.xlabel('后50个数据')
plt.ylabel('单价/（元/平方米）')
plt.title('二手房房价实际值和估计值折线图')
plt.show()